package com.darwin.darwinUnit
{
	import flash.utils.describeType;

	public class DarwinTestCase
	{
		public function DarwinTestCase(clazz:Class = null)
		{
			this.clazz = clazz;
		}
		
		protected var clazz:Class;
		protected var instance:Object;
		
		
		/**
		 * 这里执行类的构造函数
		 */
		protected function constructor():void
		{
			if(clazz) instance = new clazz();
		}
		
		/**
		 * 执行前准备
		 */
		protected function beforeClass():void
		{
			
		}
		
		public function test():void
		{
		}
		
		//测试结果存储数组
		public var result:Vector.<TestResult> = new Vector.<TestResult>();
		
		/**
		 * 存储一个测试结果 
		 * @param string 
		 */
		public function pushResult(testResult:TestResult):void
		{
			result.push(testResult);
		}
		
		
		/**
		 * trace出测试结果
		 */
		public function traceResult():void
		{
			for(var i:int = 0;i<result.length;i++)
			{
				trace(result[i].toString());
			}
		}
		
		
		/**
		 * 执行测试
		 */
		public function run():DarwinTestCase
		{
			constructor();
			beforeClass();
			
			var xml:XML = describeType(this);
			var method:Object = xml.method;
			for each(var item:Object in method)
			{
				var $name:String = item.@name.toString();
				
				if((new RegExp(/^test.*/) as Object).test($name))
				{
					this[$name]();
				}
			}
			return this;
		}
	}
}